﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Gift.DBUtility;//Please add references
namespace Gift.DAL
{
    /// <summary>
    /// 数据访问类:DALGoodsPromotion
    /// </summary>
    public partial class DALGoodsPromotion
    {
        public DALGoodsPromotion()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbGoodsPromotion");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Gift.Model.tbGoodsPromotion model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbGoodsPromotion(");
            strSql.Append("promotionTitle,promotionType,limitAmount,floorAmount,preferentialAmount,presentedIntegral,isOverlap,gift,discount,dollarAmount,startTime,endTime,sortOrder,createTime,modifyTime,companyId)");
            strSql.Append(" values (");
            strSql.Append("@promotionTitle,@promotionType,@limitAmount,@floorAmount,@preferentialAmount,@presentedIntegral,@isOverlap,@gift,@discount,@dollarAmount,@startTime,@endTime,@sortOrder,@createTime,@modifyTime,@companyId)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@promotionTitle", SqlDbType.NVarChar,50),
					new SqlParameter("@promotionType", SqlDbType.Int,4),
					new SqlParameter("@limitAmount", SqlDbType.Decimal,9),
					new SqlParameter("@floorAmount", SqlDbType.Decimal,9),
					new SqlParameter("@preferentialAmount", SqlDbType.Decimal,9),
					new SqlParameter("@presentedIntegral", SqlDbType.Int,4),
					new SqlParameter("@isOverlap", SqlDbType.Bit,1),
					new SqlParameter("@gift", SqlDbType.Int,4),
					new SqlParameter("@discount", SqlDbType.Decimal,9),
					new SqlParameter("@dollarAmount", SqlDbType.NChar,10),
					new SqlParameter("@startTime", SqlDbType.DateTime),
					new SqlParameter("@endTime", SqlDbType.DateTime),
					new SqlParameter("@sortOrder", SqlDbType.Int,4),
					new SqlParameter("@createTime", SqlDbType.DateTime),
					new SqlParameter("@modifyTime", SqlDbType.DateTime),
                   new SqlParameter("@companyId", SqlDbType.Int,4)};
            parameters[0].Value = model.promotionTitle;
            parameters[1].Value = model.promotionType;
            parameters[2].Value = model.limitAmount;
            parameters[3].Value = model.floorAmount;
            parameters[4].Value = model.preferentialAmount;
            parameters[5].Value = model.presentedIntegral;
            parameters[6].Value = model.isOverlap;
            parameters[7].Value = model.gift;
            parameters[8].Value = model.discount;
            parameters[9].Value = model.dollarAmount;
            parameters[10].Value = model.startTime;
            parameters[11].Value = model.endTime;
            parameters[12].Value = model.sortOrder;
            parameters[13].Value = model.createTime;
            parameters[14].Value = model.modifyTime;
            parameters[15].Value = model.CompanyId;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gift.Model.tbGoodsPromotion model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbGoodsPromotion set ");
            strSql.Append("promotionTitle=@promotionTitle,");
            strSql.Append("promotionType=@promotionType,");
            strSql.Append("limitAmount=@limitAmount,");
            strSql.Append("floorAmount=@floorAmount,");
            strSql.Append("preferentialAmount=@preferentialAmount,");
            strSql.Append("presentedIntegral=@presentedIntegral,");
            strSql.Append("isOverlap=@isOverlap,");
            strSql.Append("gift=@gift,");
            strSql.Append("discount=@discount,");
            strSql.Append("dollarAmount=@dollarAmount,");
            strSql.Append("startTime=@startTime,");
            strSql.Append("endTime=@endTime,");
            strSql.Append("sortOrder=@sortOrder,");
            strSql.Append("createTime=@createTime,");
            strSql.Append("modifyTime=@modifyTime,");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@promotionTitle", SqlDbType.NVarChar,50),
					new SqlParameter("@promotionType", SqlDbType.Int,4),
					new SqlParameter("@limitAmount", SqlDbType.Decimal,9),
					new SqlParameter("@floorAmount", SqlDbType.Decimal,9),
					new SqlParameter("@preferentialAmount", SqlDbType.Decimal,9),
					new SqlParameter("@presentedIntegral", SqlDbType.Int,4),
					new SqlParameter("@isOverlap", SqlDbType.Bit,1),
					new SqlParameter("@gift", SqlDbType.Int,4),
					new SqlParameter("@discount", SqlDbType.Decimal,9),
					new SqlParameter("@dollarAmount", SqlDbType.NChar,10),
					new SqlParameter("@startTime", SqlDbType.DateTime),
					new SqlParameter("@endTime", SqlDbType.DateTime),
					new SqlParameter("@sortOrder", SqlDbType.Int,4),
					new SqlParameter("@createTime", SqlDbType.DateTime),
					new SqlParameter("@modifyTime", SqlDbType.DateTime),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.promotionTitle;
            parameters[1].Value = model.promotionType;
            parameters[2].Value = model.limitAmount;
            parameters[3].Value = model.floorAmount;
            parameters[4].Value = model.preferentialAmount;
            parameters[5].Value = model.presentedIntegral;
            parameters[6].Value = model.isOverlap;
            parameters[7].Value = model.gift;
            parameters[8].Value = model.discount;
            parameters[9].Value = model.dollarAmount;
            parameters[10].Value = model.startTime;
            parameters[11].Value = model.endTime;
            parameters[12].Value = model.sortOrder;
            parameters[13].Value = model.createTime;
            parameters[14].Value = model.modifyTime;
            parameters[15].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbGoodsPromotion ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbGoodsPromotion ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.tbGoodsPromotion GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,promotionTitle,promotionType,limitAmount,floorAmount,preferentialAmount,presentedIntegral,isOverlap,gift,discount,dollarAmount,startTime,endTime,sortOrder,createTime,modifyTime from tbGoodsPromotion ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            Gift.Model.tbGoodsPromotion model = new Gift.Model.tbGoodsPromotion();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["promotionTitle"] != null && ds.Tables[0].Rows[0]["promotionTitle"].ToString() != "")
                {
                    model.promotionTitle = ds.Tables[0].Rows[0]["promotionTitle"].ToString();
                }
                if (ds.Tables[0].Rows[0]["promotionType"] != null && ds.Tables[0].Rows[0]["promotionType"].ToString() != "")
                {
                    model.promotionType = int.Parse(ds.Tables[0].Rows[0]["promotionType"].ToString());
                }
                if (ds.Tables[0].Rows[0]["limitAmount"] != null && ds.Tables[0].Rows[0]["limitAmount"].ToString() != "")
                {
                    model.limitAmount = decimal.Parse(ds.Tables[0].Rows[0]["limitAmount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["floorAmount"] != null && ds.Tables[0].Rows[0]["floorAmount"].ToString() != "")
                {
                    model.floorAmount = decimal.Parse(ds.Tables[0].Rows[0]["floorAmount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["preferentialAmount"] != null && ds.Tables[0].Rows[0]["preferentialAmount"].ToString() != "")
                {
                    model.preferentialAmount = decimal.Parse(ds.Tables[0].Rows[0]["preferentialAmount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["presentedIntegral"] != null && ds.Tables[0].Rows[0]["presentedIntegral"].ToString() != "")
                {
                    model.presentedIntegral = int.Parse(ds.Tables[0].Rows[0]["presentedIntegral"].ToString());
                }
                if (ds.Tables[0].Rows[0]["isOverlap"] != null && ds.Tables[0].Rows[0]["isOverlap"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["isOverlap"].ToString() == "1") || (ds.Tables[0].Rows[0]["isOverlap"].ToString().ToLower() == "true"))
                    {
                        model.isOverlap = true;
                    }
                    else
                    {
                        model.isOverlap = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["gift"] != null && ds.Tables[0].Rows[0]["gift"].ToString() != "")
                {
                    model.gift = int.Parse(ds.Tables[0].Rows[0]["gift"].ToString());
                }
                if (ds.Tables[0].Rows[0]["discount"] != null && ds.Tables[0].Rows[0]["discount"].ToString() != "")
                {
                    model.discount = decimal.Parse(ds.Tables[0].Rows[0]["discount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["dollarAmount"] != null && ds.Tables[0].Rows[0]["dollarAmount"].ToString() != "")
                {
                    model.dollarAmount = ds.Tables[0].Rows[0]["dollarAmount"].ToString();
                }
                if (ds.Tables[0].Rows[0]["startTime"] != null && ds.Tables[0].Rows[0]["startTime"].ToString() != "")
                {
                    model.startTime = DateTime.Parse(ds.Tables[0].Rows[0]["startTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["endTime"] != null && ds.Tables[0].Rows[0]["endTime"].ToString() != "")
                {
                    model.endTime = DateTime.Parse(ds.Tables[0].Rows[0]["endTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["sortOrder"] != null && ds.Tables[0].Rows[0]["sortOrder"].ToString() != "")
                {
                    model.sortOrder = int.Parse(ds.Tables[0].Rows[0]["sortOrder"].ToString());
                }
                if (ds.Tables[0].Rows[0]["createTime"] != null && ds.Tables[0].Rows[0]["createTime"].ToString() != "")
                {
                    model.createTime = DateTime.Parse(ds.Tables[0].Rows[0]["createTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["modifyTime"] != null && ds.Tables[0].Rows[0]["modifyTime"].ToString() != "")
                {
                    model.modifyTime = DateTime.Parse(ds.Tables[0].Rows[0]["modifyTime"].ToString());
                }
         
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,promotionTitle,promotionType,limitAmount,floorAmount,preferentialAmount,presentedIntegral,isOverlap,gift,discount,dollarAmount,startTime,endTime,sortOrder,createTime,modifyTime ");
            strSql.Append(" FROM tbGoodsPromotion ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,promotionTitle,promotionType,limitAmount,floorAmount,preferentialAmount,presentedIntegral,isOverlap,gift,discount,dollarAmount,startTime,endTime,sortOrder,createTime,modifyTime  ");
            strSql.Append(" FROM tbGoodsPromotion ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbGoodsPromotion ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from tbGoodsPromotion T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataTable GetGoodsPromotion()
        {
            DataTable table = new DataTable();
            try
            {
                string commandText = string.Format("select id,promotionTitle,promotionType,limitAmount,floorAmount,discount,presentedIntegral,isOverlap,startTime,endTime,preferentialAmount,presentedIntegral,isOverlap,gift,dollarAmount from tbGoodsPromotion  order by sortOrder", new object[0]);
                table = DbHelperSQL.Query(commandText).Tables[0];
            }
            catch
            {
            }
            return table;
        }

        public DataTable GetPresentedIntegral(int promotionType, decimal total)
        {
            DataTable table = new DataTable();
            try
            {
                string commandText = string.Format("select presentedIntegral,isOverlap from tbGoodsPromotion where promotionType=@promotionType and @total<limitAmount and @total>=floorAmount and getdate()>=startTime and getdate()<=endTime  order by sortOrder", new object[0]);
                SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@promotionType", promotionType), new SqlParameter("@total", total)};
                table = DbHelperSQL.Query(commandText, commandParameters).Tables[0];
            }
            catch
            {
            }
            return table;
        }


        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbGoodsPromotion";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

